﻿using Autofac;
using Autofac.Core;
using Hrm.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hrm.EntityFramework
{
    public class EntityFrameworkModule : IHrmModule
    {

        public void Initialize(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            builder.RegisterType<HrmDbContext>().As<IDbContext>().Named<IDbContext>("HrmDb").SingleInstance();
            builder.RegisterType<PermissionContext>().As<IDbContext>().Named<IDbContext>("Permission").SingleInstance();

            builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
                .Where(t => t.Name.EndsWith("HrmDbRepository")).WithParameter(ResolvedParameter.ForNamed<IDbContext>("HrmDb"))
                .AsImplementedInterfaces();

            builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
                .Where(t => t.Name.EndsWith("PermissionDbRepository")).WithParameter(ResolvedParameter.ForNamed<IDbContext>("Permission"))
                .AsImplementedInterfaces();
        }

        public int Order
        {
            get { return 1; }
        }
    }
}
